package com.tg.app.widget;

import android.text.TextUtils;

import com.tange.module.device.feature.DeviceFeature;
import com.tg.appcommon.android.TGLog;

public class ZoomHelper {
    private float mMaxFactor; //最大缩放倍数
    private float mBorderFactor;//切换边界倍数

    public int getIndex() {
        return mIndex;
    }

    private int mIndex = 0;

    public int getStep() {
        return mStep;
    }

    private int mStep = 0;

    public float getZoomFactor() {
        return mZoomFactor;
    }

    public void setZoomFactor(float zoomFactor) {
        mZoomFactor = zoomFactor;
    }

    private float mZoomFactor; //当前缩放倍数

    public String getText() {
        return mText;
    }

    private String mText;

    public ZoomHelper() {
        init();
    }

    public float getProgress(float zoomFactor) {
        float progress = zoomFactor * (mMaxFactor - 1.0f) + 1.0f;
        TGLog.d("zoomFactor = " + zoomFactor + ", progress = " + progress);
        return progress;
    }

    public boolean isFar(float zoomFactor) {
        float progress = getProgress(zoomFactor);
        TGLog.d("mBorderFactor = " + mBorderFactor);
        return progress > mBorderFactor;
    }

    public float getMaxFactor() {
        return mMaxFactor;
    }

    public float getBorderFactor() {
        return mBorderFactor;
    }

    private void init() {
        mText = "";
        mStep = 0;
        mMaxFactor = 0.0f;
        mBorderFactor = 0.0f;
        mZoomFactor = 0.0f;
        mIndex = 0;
    }

    public void parseParam(DeviceFeature.ZoomFeature zoomFeature) {
        init();
        if (zoomFeature ==null) {
            return;
        }
        mMaxFactor = zoomFeature.maxFactor;
        mBorderFactor = zoomFeature.startFactor;
        mStep = zoomFeature.steps-1;
        mIndex = zoomFeature.index;
        TGLog.d("step = " + mStep + ", maxFactor = " + mMaxFactor + ", mBorderFactor = " + mBorderFactor);
    }

    public void parseParam(String text){
        init();
        if (TextUtils.isEmpty(text)) {
            return;
        }
        text = text.trim();
        text = text.replace(" ", "");
        text = text.replace("times:", "");
        String[] src = text.split(",");
        if (src.length != 3) {
            return;
        }
        mText = text;
        String start = src[0].replace("L","").replace("E","");
        String end = src[1].replace("L","").replace("E","");
        TGLog.d(" src[0] = " +  src[0] + ",  src[1] = " +  src[1] + ",  src[2] = " +  src[2]);
        parseParamImpl(start, end, src[2]);
    }

    private void parseParamImpl(String start, String end, String steps){
        float  startFloat = Float.parseFloat(start);
        mMaxFactor = startFloat * Float.parseFloat(end);
        mBorderFactor = startFloat;
        mStep = (int)Float.parseFloat(steps) - 1;
        TGLog.d("step = " + mStep + ", maxFactor = " + mMaxFactor + ", mBorderFactor = " + mBorderFactor);
    }
}
